package cn.yao.jobclient.rest;

import cn.yao.jobclient.config.RestTemplateConfig;
import cn.yao.jobclient.controller.TestController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.exception.HystrixBadRequestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: Admin
 * @Date: 2019-02-27 09:43
 */
@Service
//@RefreshScope //开启更新功能
public class JobRest {

//    @Value("${rest.job.url}")
//    private String jobUrl;

    private final static String  JOB_URL ="http://job/";

    @Autowired
    public RestTemplate restTemplate;

    private static Logger logger = LoggerFactory.getLogger(JobRest.class);
//ArithmeticException
    @HystrixCommand(fallbackMethod = "error1",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "11000")//降级时间时间单位为毫秒 默认请求次数超过50%自行熔断
    })
    public String restJobService(){
        logger.info("远程调用开始");
//        int i = 1/0;
        String resp="hello";
//        try {
            resp = restTemplate.getForObject(JOB_URL+"index",String.class);
//        }catch (Exception e){
//            logger.error("错误：",e);
//        }

        return resp;
    }

    public String error1(Throwable e){
        logger.error("错误：",e);
        return "{\"code:\":\""+e.getMessage()+"\"}";
    }

    public String fallback2(){
        return "{\"code:\":\"111\"}";
    }

}
